package com.wondersgroup.framework.widget.taglib.form;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.form.ExtCheckbox;
import com.wondersgroup.framework.widget.library.extjs.form.ExtCheckboxGroup;
import com.wondersgroup.framework.widget.library.extjs.form.ExtField;
import com.wondersgroup.framework.widget.library.extjs.form.ExtRadio;
import com.wondersgroup.framework.widget.library.extjs.form.ExtRadioGroup;
import com.wondersgroup.framework.widget.library.html.Div;
import com.wondersgroup.framework.widget.library.html.HtmlComponent;
import com.wondersgroup.framework.widget.taglib.form.util.FormUtils;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TagValidate;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;
import com.wondersgroup.framework.widget.validation.annotation.ValueValidate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

@WidgetTag(name = "checkboxGroup", tldTagClass = "com.wondersgroup.framework.widget.taglib.form.CheckboxGroupTag", description = "CheckBoxGroup和checkbox的最大区别是checkboxgroup关注勾选了哪几项，checkbox关注勾选的动作")
@TagValidate
public class CheckboxGroupTag extends RequiredTag {
	public static final String CHECK_ITEMS = "checkItems";

	@TypeValidate
	private boolean checkAll;
	private String checkAllText = "全选";
	private String columns;
	private int maxSelect = 2;
	private int minSelect = 1;
	private String width = "0";

	@ValueValidate({ "radio", "checkbox" })
	private String type = "checkbox";
	private String dataFields;
	private String data;
	private String url;
	private String urlParams;
	private String valueField = "value";
	private String displayField = "text";

	@TypeValidate
	private boolean enableTooltip;

	public void processStartTagContent() throws JspException {
		ArrayList localArrayList = new ArrayList();
		this.pageContext.setAttribute("checkItems", localArrayList);
	}

	public void processEndTagContent() throws JspException {
		Object localObject1;
		Object localObject2;
		Object localObject3;
		CheckboxTag localCheckboxTag;
		Div localDiv = new Div(getId() + "_div");
		if (StringUtils.isEmpty(getWidth()) || ("0".equals(getWidth()))) {
			setAutoWidth(true);
		}

		StringBuffer localStringBuffer = new StringBuffer();
		List localList = (List) this.pageContext.getAttribute("checkItems");
		if ((getType() != null) && (getType().equals("checkbox"))) {
			localObject1 = new ExtCheckboxGroup(getId());
			((ExtCheckboxGroup) localObject1).setFormItems(getFormItems());
			BeanUtils.copyProperties(this, localObject1);
			if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
				((ExtCheckboxGroup) localObject1).setRenderTo(localDiv.getId());
			}
				
			if ((getUrl() == null) || (!(getUrl().equals("")))) {
				localObject2 = null;
				if (isCheckAll()) {
					localObject3 = new StringBuffer();
					((StringBuffer) localObject3).append("{");
					((StringBuffer) localObject3).append("id:\"").append(getId()).append("All").append("\"").append(",");
					((StringBuffer) localObject3).append("boxLabel:\"").append(getCheckAllText()).append("\"").append(",");
					((StringBuffer) localObject3).append("autoWidth:").append("true").append(",");
					((StringBuffer) localObject3)
							.append("listeners:{")
							.append("'check':function(t,c){Ext.getCmp('")
							.append(getId())
							.append("').childItems.each(function(item){if(item.getValue()!=c)item.setValue(c);})}}")
							.append(",");
					((StringBuffer) localObject3).append("renderTo:\"").append(getId()).append("All_div").append("\"");
					((StringBuffer) localObject3).append("}");
					localObject2 = new StringBuffer(((StringBuffer) localObject3).toString().length() + 2);
					((StringBuffer) localObject2).append("[");
					((StringBuffer) localObject2).append(((StringBuffer) localObject3).toString()).append(",");
					localObject3 = null;
				} else {
					localObject2 = new StringBuffer("[");
				}
				localObject3 = localList.iterator();
				while (((Iterator) localObject3).hasNext()) {
					localCheckboxTag = (CheckboxTag) ((Iterator) localObject3).next();
					((StringBuffer) localObject2).append(localCheckboxTag.getOutputContent()).append(",");
				}
				
				if (((StringBuffer) localObject2).toString().length() == 1) {
					((ExtCheckboxGroup) localObject1).setItems("");
				} else {
					((ExtCheckboxGroup) localObject1).setItems(((StringBuffer) localObject2).toString().substring(0, ((StringBuffer) localObject2).toString().length() - 1) + "]");
				}
					
			}
			localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
			localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/form/wonders-form-min.js" }));
			localStringBuffer.append("<script>cf_r(function(){");
			localStringBuffer.append(((ExtCheckboxGroup) localObject1).getJS());
			localObject2 = localList.iterator();
			while (((Iterator) localObject2).hasNext()) {
				localObject3 = (CheckboxTag) ((Iterator) localObject2).next();
				if (StringUtils.hasText(((CheckboxTag) localObject3).getOnCheck())) {
					localStringBuffer.append("Ext.getCmp('");
					localStringBuffer.append(((CheckboxTag) localObject3).getId());
					localStringBuffer.append("').on('check',function(t,c){");
					localStringBuffer.append(((CheckboxTag) localObject3).getOnCheck() + "});");
				}
			}
			localStringBuffer.append("});</script>");
			FormUtils.addFieldsToForm(this.pageContext, (ExtField) localObject1);
		} else {
			localObject1 = new ExtRadioGroup(getId());
			BeanUtils.copyProperties(this, localObject1);
			((ExtRadioGroup) localObject1).setFormItems(getFormItems());
			if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
				((ExtRadioGroup) localObject1).setRenderTo(localDiv.getId());
			}
			
			if ((getUrl() == null) || (!(getUrl().equals("")))) {
				localObject2 = new StringBuffer("[");
				localObject3 = localList.iterator();
				while (((Iterator) localObject3).hasNext()) {
					localCheckboxTag = (CheckboxTag) ((Iterator) localObject3).next();
					((StringBuffer) localObject2).append(localCheckboxTag.getOutputContent()).append(",");
				}
				if (((StringBuffer) localObject2).toString().length() == 1) {
					((ExtRadioGroup) localObject1).setItems("");
				} else {
					((ExtRadioGroup) localObject1).setItems(((StringBuffer) localObject2).toString().substring(0,((StringBuffer) localObject2).toString().length() - 1) + "]");
				}
					
			}
			localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
			localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/form/wonders-form-min.js" }));
			localStringBuffer.append("<script>cf_r(function(){");
			localStringBuffer.append(((ExtRadioGroup) localObject1).getJS());
			localObject2 = localList.iterator();
			while (((Iterator) localObject2).hasNext()) {
				localObject3 = (CheckboxTag) ((Iterator) localObject2).next();
				if ((StringUtils.hasText(((CheckboxTag) localObject3).getOnCheck())) && (StringUtils.hasText(((CheckboxTag) localObject3).getOnCheck()))) {
					localStringBuffer.append("Ext.getCmp('");
					localStringBuffer.append(((CheckboxTag) localObject3).getId());
					localStringBuffer.append("').on('check',function(t,c){");
					localStringBuffer.append(((CheckboxTag) localObject3).getOnCheck() + "});");
				}
			}
			localStringBuffer.append("});</script>");
			FormUtils.addFieldsToForm(this.pageContext, (ExtField) localObject1);
		}
		TagUtils.getInstance().write(this.pageContext,localStringBuffer.toString());
		this.pageContext.removeAttribute("checkItems");
	}

	private String getAllCheckboxContent(CheckboxTag paramCheckboxTag) throws JspException {
		StringBuffer localStringBuffer1 = new StringBuffer();
		StringBuffer localStringBuffer2 = new StringBuffer(paramCheckboxTag.getClass().getSimpleName()).append("_").append(getUUID());
		paramCheckboxTag.setId(localStringBuffer2.toString());
		Div localDiv = new Div(paramCheckboxTag.getId() + "_div");
		ExtCheckbox localExtCheckbox = new ExtCheckbox(paramCheckboxTag.getId());
		localExtCheckbox.setText(getCheckAllText());
		localExtCheckbox.setRenderTo(localDiv.getId());
		localExtCheckbox.setFormItems(getFormItems());
		localExtCheckbox.setOnCheck(paramCheckboxTag.getOnCheck());
		localStringBuffer1.append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
		localStringBuffer1.append(OutputUtils.ExtOnReady(new ExtComponent[] { localExtCheckbox }));
		return localStringBuffer1.toString();
	}

	private String getSingleCheckboxContent(CheckboxTag paramCheckboxTag) throws JspException {
		Object localObject1;
		Object localObject2;
		StringBuffer localStringBuffer = new StringBuffer();
		if (paramCheckboxTag.getId() == null) {
			localObject1 = new StringBuffer(paramCheckboxTag.getClass().getSimpleName()).append("_").append(getUUID());
			paramCheckboxTag.setId(((StringBuffer) localObject1).toString());
		}
		localObject1 = new Div(paramCheckboxTag.getId() + "_div");
		if ((getType() != null) && (getType().equals("checkbox"))) {
			localObject2 = new ExtCheckbox(paramCheckboxTag.getId());
			((ExtCheckbox) localObject2).setProperty(getProperty());
			((ExtCheckbox) localObject2).setText(paramCheckboxTag.getText());
			((ExtCheckbox) localObject2).setValue(paramCheckboxTag.getValue());
			((ExtCheckbox) localObject2).setInputValue(paramCheckboxTag.getInputValue());
			((ExtCheckbox) localObject2).setChecked(paramCheckboxTag.isChecked());
			((ExtCheckbox) localObject2).setDisabled(paramCheckboxTag.isDisabled());
			((ExtCheckbox) localObject2).setReadOnly(paramCheckboxTag.isReadOnly());
			((ExtCheckbox) localObject2).setWidth(paramCheckboxTag.getWidth());
			((ExtCheckbox) localObject2).setAutoWidth(paramCheckboxTag.isAutoWidth());
			((ExtCheckbox) localObject2).setRenderTo(((Div) localObject1).getId() + "_div");
			((ExtCheckbox) localObject2).setFormItems(getFormItems());
			((ExtCheckbox) localObject2).setOnCheck(paramCheckboxTag.getOnCheck());
			((ExtCheckbox) localObject2).setCls(paramCheckboxTag.getCls());
			((ExtCheckbox) localObject2).setAllowBlank(paramCheckboxTag.isAllowBlank());
			localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { (Div) localObject1 }));
			localStringBuffer.append(OutputUtils.ExtOnReady(new ExtComponent[] { (ExtCheckbox) localObject2 }));
		}
		if ((getType() != null) && (getType().equals("radio"))) {
			localObject2 = new ExtRadio(paramCheckboxTag.getId());
			((ExtRadio) localObject2).setProperty(getProperty());
			((ExtRadio) localObject2).setText(paramCheckboxTag.getText());
			((ExtRadio) localObject2).setValue(paramCheckboxTag.getValue());
			((ExtRadio) localObject2).setInputValue(paramCheckboxTag.getInputValue());
			((ExtRadio) localObject2).setChecked(paramCheckboxTag.isChecked());
			((ExtRadio) localObject2).setDisabled(paramCheckboxTag.isDisabled());
			((ExtRadio) localObject2).setReadOnly(paramCheckboxTag.isReadOnly());
			((ExtRadio) localObject2).setWidth(paramCheckboxTag.getWidth());
			((ExtRadio) localObject2).setAutoWidth(paramCheckboxTag.isAutoWidth());
			((ExtRadio) localObject2).setRenderTo(((Div) localObject1).getId() + "_div");
			((ExtRadio) localObject2).setFormItems(getFormItems());
			((ExtRadio) localObject2).setOnCheck(paramCheckboxTag.getOnCheck());
			((ExtRadio) localObject2).setCls(paramCheckboxTag.getCls());
			((ExtRadio) localObject2).setAllowBlank(paramCheckboxTag.isAllowBlank());
			localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { (Div) localObject1 }));
			localStringBuffer.append(OutputUtils.ExtOnReady(new ExtComponent[] { (ExtRadio) localObject2 }));
		}
		return ((String) (String) localStringBuffer.toString());
	}

	public boolean isCheckAll() {
		return this.checkAll;
	}

	@WidgetTagAttribute(description = "是否添加一个全选checkbox，默认为false，当type='checkbox'时有效")
	public void setCheckAll(boolean paramBoolean) {
		this.checkAll = paramBoolean;
	}

	public String getCheckAllText() {
		return this.checkAllText;
	}

	@WidgetTagAttribute(description = "设置checkAll=true时，新加checkbox的文本,默认为'全选'")
	public void setCheckAllText(String paramString) {
		this.checkAllText = paramString;
	}

	public String getColumns() {
		return this.columns;
	}

	public void setColumns(String paramString) {
		this.columns = paramString;
	}

	public int getMaxSelect() {
		return this.maxSelect;
	}

	public void setMaxSelect(int paramInt) {
		this.maxSelect = paramInt;
	}

	public int getMinSelect() {
		return this.minSelect;
	}

	public void setMinSelect(int paramInt) {
		this.minSelect = paramInt;
	}

	public String getType() {
		return this.type;
	}

	@WidgetTagAttribute(description = "设置该组包含的所有选择框的渲染类型---包括：radio(单选)、checkbox(复选)，默认为checkbox(复选)", defaultValue = "checkbox")
	public void setType(String paramString) {
		this.type = paramString;
	}

	public String getDataFields() {
		return this.dataFields;
	}

	@WidgetTagAttribute(description = "用于做多维数组或JSON数据的映射，用于静态数据的加载，remote=false时有效")
	public void setDataFields(String paramString) {
		this.dataFields = paramString;
	}

	public String getValueField() {
		return this.valueField;
	}

	@WidgetTagAttribute(description = "列表中所有项目的值域，默认为'value'")
	public void setValueField(String paramString) {
		this.valueField = paramString;
	}

	public String getDisplayField() {
		return this.displayField;
	}

	@WidgetTagAttribute(description = "列表中所有项目的显示文本域，默认为'text'")
	public void setDisplayField(String paramString) {
		this.displayField = paramString;
	}

	public String getData() {
		return this.data;
	}

	@WidgetTagAttribute(description = "列表的静态数据，remote=false时有效")
	public void setData(String paramString) {
		this.data = paramString;
	}

	public String getUrl() {
		return this.url;
	}

	@WidgetTagAttribute(description = "用于加载列表的远程URL，当remote=true时有效")
	public void setUrl(String paramString) {
		this.url = paramString;
	}

	public String getUrlParams() {
		return this.urlParams;
	}

	@WidgetTagAttribute(description = "用于指定远程URL的额外参数，当remote=true时有效")
	public void setUrlParams(String paramString) {
		this.urlParams = paramString;
	}

	public String getWidth() {
		return this.width;
	}

	@WidgetTagAttribute(description = "自定义组件宽度")
	public void setWidth(String paramInt) {
		this.width = paramInt;
	}

	public boolean isEnableTooltip() {
		return this.enableTooltip;
	}

	@WidgetTagAttribute(description = "是否启用提示注释，默认为false")
	public void setEnableTooltip(boolean paramBoolean) {
		this.enableTooltip = paramBoolean;
	}
}